<?php

namespace App\Remote\Feishu\User;

use App\Exceptions\BaseException;
use Closure;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class UserRemote extends Remote
{

    const TYPE_USER_GET = "open_id";

    /**
     * 获取用户信息
     *
     * @param string $userType
     * @param string $ssoToken
     * @param string $openId
     * @return mixed|void
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function userinfoBySsoToken(string $userType, string $ssoToken, string $openId)
    {
        return self::customRequest(
            "/open-apis/contact/v3/users/{$openId}",
            RequestAlias::METHOD_GET,
            [
                "user_id_type" => $userType
            ],
            ["Authorization" => sprintf("Bearer %s", $ssoToken)]
        );
    }

    /**
     * @param string $departmentId
     * @param string $tenantAccessToken
     * @param string $pageToken
     * @return mixed|void
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function getUserByDepartment(string $departmentId, string $tenantAccessToken, string $pageToken = "")
    {
        return self::customRequest(
            "/open-apis/contact/v3/users/find_by_department",
            RequestAlias::METHOD_GET,
            [
                "department_id_type" => "open_department_id",
                "department_id"      => $departmentId,
                "page_size"          => 50,
                "page_token"         => $pageToken
            ],
            ["Authorization" => sprintf("Bearer %s", $tenantAccessToken)]
        );
    }

    public static function registerResponseHandle(): Closure
    {
        return function (string $data) {
            return json_decode($data, JSON_UNESCAPED_UNICODE)["data"];
        };
    }
}
